package UnderAccessDBclass;

import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class jdbcAssistance {
    /**
     * 解析元数据的结构
     *
     * @param metaData 元数据
     * @return 结果集每个列的数据类型和列名
     */
    public static List<ColumnInfo> parseMetaData(ResultSetMetaData metaData) {
        ArrayList<ColumnInfo> list = new ArrayList<ColumnInfo>();
        try {
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                ColumnInfo col = new ColumnInfo(metaData.getColumnName(i + 1), metaData.getColumnType(i + 1));
                list.add(col);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 判断是否是整数
     *
     * @param num 数字
     * @return true表示为int false表示为浮点值
     */
    public static boolean isInteger(BigDecimal num) {
        //判断数值是否有小数点
        return !num.toString().contains(".");
    }

    /**
     * 对首字母进行大写转换的方法
     *
     * @param fieldName 字段名
     */
    public static String parseFirstUpper(String fieldName) {
        char first = fieldName.charAt(0);
        first = (char) ((int) first - 32);
        return first + fieldName.substring(1);
    }

    public static Date stringToDate(String time, String pattern) throws ParseException {
        return new SimpleDateFormat(pattern).parse(time);
    }

    public static String dateToString(Date time, String pattern) {
        return new SimpleDateFormat(pattern).format(time);
    }

    public static Date getDateByFormat(Date time, String pattern) throws ParseException {
        return stringToDate(dateToString(time, pattern), "yyyy-MM-dd");
    }

    public static Calendar dateTocalendar(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        return cal;
    }

    public static boolean valitadeIsWeekend(Calendar cal) {
        return cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY;
    }
}
